package com.lifa.controller;


import com.lifa.entity.Permission;
import com.lifa.entity.UserRole;
import com.lifa.entity.Userinfo;
import com.lifa.service.UserInfoService;
import com.lifa.dao.PermissionDao;
import com.lifa.dao.RolePermissionDao;
import com.lifa.dao.UserInfoDao;
import com.lifa.dao.UserRoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
@Scope("prototype")
public class UserInfoController {
    @Autowired
    private UserInfoDao userInfoDao ;
    @Autowired
    private UserInfoService userInfoService ;
    @Autowired
    private PermissionDao permissionDao ;
    @Autowired
    private RolePermissionDao rolePermissionDao ;
    @Autowired
    private UserRoleDao userRoleDao ;
    @RequestMapping("/login")
    public String login(String username, String pwd, Model model, HttpServletRequest request){
        Userinfo userinfo = userInfoDao.login(username,pwd) ;
        request.getSession().setAttribute("userinfo",userinfo);
        UserRole userRole = userRoleDao.getRoleByUserId((int) userinfo.getId()) ;
        List<Integer> list = rolePermissionDao.getPermissionByRoleId((int)userRole.getRoleid()) ;
        List<Permission> list1 = permissionDao.queryByPermissionIds(list) ;

        List<Permission> list2 = new ArrayList<>() ;
        for(Permission p : list1){
            if(p.getParentid() ==0) {
                list2.add(p);
            }
        }
        model.addAttribute("list",list2) ;
        model.addAttribute("list1",list1) ;
        return "index" ;
    }
@RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("userinfo");
        return "redirect:/login.jsp" ;
    }

    @RequestMapping("/getAll")
    @ResponseBody
    public List<Userinfo> getAll() {
        return userInfoService.getAll() ;
    }

    @RequestMapping("/queryAll")
    @ResponseBody
    public Map<String,Object> queryAll(int offset, int limit){
       /* Map<String,Object> map = new HashMap<>() ;
        map.put("rows",userInfoService.findAll(offset,limit)) ;
        map.put("total",userInfoService.findTotalCount()) ;
        return  map ;*/

       return  null ;
    }

    @RequestMapping("/querySearch")
    @ResponseBody
    public Map<String,Object> querySearch(String username,int offset, int limit){

        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",userInfoService.findAll(username,offset,limit)) ;
        map.put("total",userInfoService.findTotalCount(username)) ;
        return  map ;

    }
    @RequestMapping("/doFenPei")
    @ResponseBody
    public   Map<String,Object> doFenPei(int userinfoid,int roleid){
        Map<String,Object> map = new HashMap<>() ;
        userRoleDao.update(userinfoid,roleid) ;
        map.put("rows",userInfoService.findAll("",0,2)) ;
        map.put("total",userInfoService.findTotalCount("")) ;
        return map;
    }
}
